#include "CollectChannelCountForm.h"
#include "ui_CollectChannelCountForm.h"
#include "./toObject/ModelXml.h"
CollectChannelCountForm::CollectChannelCountForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CollectChannelCountForm)
{
    ui->setupUi(this);

    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //不可编辑

    QStringList arrayHead = {"设备ID","设备名称","设备描述","所属通道ID","所属通道名称","所属通道描述","协议名称","接口类型"};
    ui->tableWidget->setColumnCount(arrayHead.size());
    ui->tableWidget->setHorizontalHeaderLabels(arrayHead);//表头
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows);
}

CollectChannelCountForm::~CollectChannelCountForm()
{
    delete ui;
}

void CollectChannelCountForm::InitUiData()
{

    std::map<int,stCollectChannel> channels = ModelXml::GetInstance()->GetCollectChannel();
    ui->tableWidget->setRowCount(0);
    for(std::map<int,stCollectChannel>::iterator it = channels.begin() ; it != channels.end() ; it++)
    {
        QStringList rowData;
        rowData.push_back(QString::number(it->second.id));
        rowData.push_back(it->second.name.c_str());
        rowData.push_back(it->second.desc.c_str());
        rowData.push_back(it->second.protoName.c_str());

        stCollectChannel::ChannelPortType portType = it->second.portType;

        std::map<int, stCollecDevice> devs = ModelXml::GetInstance()->GetCollectDevice(it->second.id);
        std::map<int,stCollecDevice>::iterator itDev = devs.begin();
        for(; itDev != devs.end() ; itDev++)
        {
            int rows = ui->tableWidget->rowCount();
            ui->tableWidget->setRowCount(rows + 1);
            ui->tableWidget->setItem(rows,0 , new QTableWidgetItem(QString::number(itDev->second.id)));
            ui->tableWidget->setItem(rows,1 , new QTableWidgetItem(itDev->second.name.c_str()));
            ui->tableWidget->setItem(rows,2 , new QTableWidgetItem(itDev->second.desc.c_str()));
            ui->tableWidget->setItem(rows,3 , new QTableWidgetItem(rowData.at(0)));
            ui->tableWidget->setItem(rows,4 , new QTableWidgetItem(rowData.at(1)));
            ui->tableWidget->setItem(rows,5 , new QTableWidgetItem(rowData.at(2)));
            ui->tableWidget->setItem(rows,6 , new QTableWidgetItem(rowData.at(3)));

            if(portType == stCollectChannel::ChannelPortType::Port_SerialPort)
                ui->tableWidget->setItem(rows,7 , new QTableWidgetItem("串口"));
            else
            {
                if(itDev->second.InterfaceType == stCollecDevice::Intarface_Type::Intarface_TcpClinet)
                {
                    ui->tableWidget->setItem(rows,7 , new QTableWidgetItem("TCP客户端"));
                }else if(itDev->second.InterfaceType == stCollecDevice::Intarface_Type::Intarface_TcpServer)
                {
                    ui->tableWidget->setItem(rows,7 , new QTableWidgetItem("TCP服务端"));
                }
                else if(itDev->second.InterfaceType == stCollecDevice::Intarface_Type::Intarface_Udp)
                {
                    ui->tableWidget->setItem(rows,7 , new QTableWidgetItem("UDP"));
                }
                else if(itDev->second.InterfaceType == stCollecDevice::Intarface_Type::Intarface_Comm)
                {
                    ui->tableWidget->setItem(rows,7 , new QTableWidgetItem("串口"));
                }
            }
        }
    }
}
